package com.NyCloud.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.NyCloud.entity.Demand;
import com.NyCloud.entity.Product;
import com.NyCloud.service.IProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mockito.internal.stubbing.answers.Returns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.MethodWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-07-26
 */
@RestController
@RequestMapping("/product")
@Api(tags = "农产品")
@CrossOrigin
public class ProductController {


    @Autowired
    private IProductService productService;

    @ApiOperation("全数据列表")
    @GetMapping("/all")
    public SaResult list() {
        List<Product> products = productService.list();
        return SaResult.data(products);
    }

    @ApiOperation("某农场的产品")
    @GetMapping("/{farmerId}")
    public SaResult getProductByFarmerId(@PathVariable int farmerId) {
        List<Product> products = productService.list(new QueryWrapper<Product>()
                .eq("user_id", farmerId));
        return SaResult.data(products);
    }

    @PostMapping
    @ApiOperation("新增产品供应")
    @SaCheckLogin
    public SaResult addProduct(@RequestBody  Product product) {
        product.setUserId(StpUtil.getLoginIdAsInt());
        productService.save(product);
        return SaResult.data(product.getId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除产品供应")
    public SaResult deleteProduct(@PathVariable String id) {
        productService.removeById(Integer.parseInt(id));
        return SaResult.ok();
    }
}
